package qgs.zisu.javaconcurrency.javaconcurrency.chapter5_join;

/**
 * @Author: 蔡翔
 * @Date: 2019/9/24 15:09
 * @Version 1.0
 */
public class ThreadJoin3   {
    public static void main(String[] args) throws InterruptedException {
        long startTimestamp = System.currentTimeMillis();
        Thread t1 = new Thread(new CaptureRunnable("M1",10000));
        Thread t2 = new Thread(new CaptureRunnable("M2",1000));
        Thread t3 = new Thread(new CaptureRunnable("M3",15000));

        t1.start();
        t2.start();
        t3.start();

        // join 打在3个start之后，就说明前三个线程是并发执行的。
        t1.join();
        t2.join();
        t3.join();
        long endTimestamp = System.currentTimeMillis();
        System.out.printf("save data begin timestap is: %s,end timestap is :%s",startTimestamp,endTimestamp);
    }
}

class CaptureRunnable implements Runnable{
    private String machineName;
    private long spendTime;

    public CaptureRunnable(String machineName,long spendTime){
        this.machineName = machineName;
        this.spendTime = spendTime;
    }

    @Override
    public void run(){
        try {
            Thread.sleep(spendTime);
            System.out.println(machineName+"completed data capture and successful") ;
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public String getResult(){
        return machineName+" finish.";
    }
}
